package es.us.lsi.daw.dao.mongo;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import es.us.lsi.daw.dao.impl.DAOComunImpl;
import es.us.lsi.daw.dao.repository.ITweetRepository;
import es.us.lsi.daw.dao.repository.ITwitterUserRepository;
import es.us.lsi.daw.dto.DTODatoGrafica;
import es.us.lsi.daw.dto.DTOTweet;
import es.us.lsi.daw.model.Tweet;
import es.us.lsi.daw.model.TwitterUser;

@Repository("daoTwitter")
@Transactional
public class DAOTwitter {

	public static final Long millisInDay = 86400000L;

	@Autowired
	private ITweetRepository tweetRepository;

	@Autowired
	private ITwitterUserRepository twitterUserRepository;

	@Resource(name = "mongoTemplate")
	private MongoTemplate mongoTemplate;

	public Tweet getTweetFromIdTwitter(Long id) {

		Query query = new Query(Criteria.where("id_twitter").is(id));
		Tweet tweet = mongoTemplate.findOne(query, Tweet.class);

		return tweet;
	}

	public List<DTODatoGrafica> getLstDTODatoGrafica(Long startDate,
			Long endDate) {

		Long auxDate = startDate;
		List<DTODatoGrafica> lstDtoDatoGrafica = new ArrayList<DTODatoGrafica>();
		
		long tTotal = 0L;

		System.out.println("MongoDB");
		while (auxDate < endDate) {

			Long auxDate2 = auxDate + millisInDay;
			
			Date start = new Date(auxDate);
			Date end = new Date(auxDate2);
			Query query = new Query(Criteria.where("publish_date").gte(start)
					.lte(end));

			long tInit = System.currentTimeMillis();
			Long count = mongoTemplate.count(query, "tweet");
			long tEnd1 = System.currentTimeMillis();

			System.out.println(auxDate+";"+auxDate2+";"+count+";"+ (tEnd1 - tInit));
			tTotal += (tEnd1 - tInit);

			DTODatoGrafica dtoDatoGrafica = new DTODatoGrafica();
			dtoDatoGrafica.setDateStart(auxDate);
			dtoDatoGrafica.setDateEnd(auxDate2);
			dtoDatoGrafica.setNumTweets(count);

			auxDate = auxDate2;

			lstDtoDatoGrafica.add(dtoDatoGrafica);
		}
		
		System.out.println("Tiempo total: "+tTotal);

		return lstDtoDatoGrafica;
	}

	public List<DTOTweet> getLstDTOTweet(Long startDate, Long endDate,
			Integer page, Integer numElements) {

		Date start = new Date(startDate);
		Date end = new Date(endDate);
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");

		PageRequest request = new PageRequest(page, numElements, new Sort(
				"publish_date", "asc"));
		List<DTOTweet> lstDTOTweet = new ArrayList<DTOTweet>();
		List<Tweet> lstTweet = tweetRepository.findBetweenDatePageable(start,
				end, request);
		for (Tweet tweet : lstTweet) {
			DTOTweet dto = new DTOTweet();
			dto.setId(tweet.getId_twitter());
			dto.setPublishDate(dateFormat.format(tweet.getPublish_date()));
			dto.setText(tweet.getText());
			TwitterUser user = twitterUserRepository.findByTwitterId(tweet
					.getAuthor());
			if (user != null) {
				dto.setUsername(user.getUser_name());
			} else {
				dto.setUsername("Desconocido");
			}
			lstDTOTweet.add(dto);
		}

		return lstDTOTweet;
	}

	
	public List<DTOTweet> getLstDTOTweetByQuery(String authorQuery,
			String textQuery,Integer page, Integer numElements) {
		
		 Criteria criteria = Criteria.where("text").regex(textQuery);
		   Query query = Query.query(criteria);
		   // apply pagination, sorting would also be specified here
		   query.limit(numElements);
		   query.skip(page);
		   List<Tweet> lstTweet = mongoTemplate.find(query, Tweet.class);
		   
		   return null;
	}
}
